/**/

#include "administrator.hpp"
#include "edge.hpp"
#include "node.hpp"

Administrator::Administrator()
{
}

Administrator::Administrator(std::string &fileToParseN, std::string &fileToParseE,std::string &fileConfig,
							 int numberOfFieldsN, int numberOfFieldsE,Logger *logger)
{
	this->create(fileToParseN,fileToParseE,fileConfig,numberOfFieldsN,numberOfFieldsE, logger);
}

void Administrator::create(std::string &fileToParseN, std::string &fileToParseE,std::string &fileConfig, int numberOfFieldsN, int numberOfFieldsE,Logger *logger)
{
	this->logger = logger;

	GraphFactory graphFactory(fileToParseN,fileToParseE,fileConfig,this->logger,numberOfFieldsN,numberOfFieldsE,&this->graph);

	graphFactory.generateGraph();
}

int Administrator::searchPath(std::string originId, std::string destinyId, int priority)
{
	Node *nodeO = this->graph.getNode(originId);
	Node *nodeD = this->graph.getNode(destinyId);
	if((nodeO)&&(nodeD))
	{

		SearchAlgorithm searchAlgorithm(this->logger);
		std::string path;
		if(searchAlgorithm.execute(&this->graph,nodeO,nodeD,priority)>=0)
		{
			Node *destiny = this->graph.getNode(destinyId);
			Edge *previous = destiny->getMinimunPathEdge();
			path="Nodo "+destinyId;
			while(previous != NULL)
			{	
				std::string prevOriginId = previous->getOriginId();
				std::string prevId = previous->getId();

				path="Nodo "+prevOriginId+" -> Arista "+ prevId +" -> "+path;

				destiny = this->graph.getNode(prevOriginId);
				previous = destiny->getMinimunPathEdge();
			}
			this->pathFound=path;
			return 1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		std::string detail = "Nodo ";
		if (!nodeO)
		{
			detail += "origen inexistente";
			this->errorCode=10;
		}
		else
		{
			detail += "destino inexistente";
			this->errorCode=20;
		}
		this->logger->reportErrorMessage("SearchAlgorithm","execute",detail);

		return -1;

	}

}

bool Administrator::isGraphCreated(){
    if (this->graph.getNumberOfNodes() > 0)
        return true;
    return false;
}

void Administrator::cleanBestPathValues()
{
	this->graph.cleanBestPathValues();
}

string Administrator::getPath()
{
	return this->pathFound;
}

int Administrator::getErrorCode()
{
	return this->errorCode;
}




Administrator::~Administrator(){}
